package com.lanju.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lanju.dao.SharePrizeDao;
import com.lanju.entity.Money;
import com.lanju.entity.SearchLog;
import com.lanju.entity.SharePrize;
import com.lanju.service.SharePrizeService;

@Service
@Transactional(value = "financialmarketingTM", rollbackFor = Exception.class)
public class SharePrizeServiceImpl implements SharePrizeService {

	@Autowired
	SharePrizeDao sharePrizeDao;

	@Override
	public Map<String, Object> insertSharePrize(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<SharePrize> list=sharePrizeDao.hasSharePrize(map.get("phone").toString());
		if(list ==null ){
			result.put("success", false);
			return result;
		}
		try {
			SharePrize sp = new SharePrize();
			sp.setPrize_time(new Date());
			sp.setPhone(map.get("phone").toString());
			sharePrizeDao.insertSharePrize(sp);
			Money m=sharePrizeDao.hasMoney(map.get("phone").toString(),1);
			if(m!=null){
				m.setPrice(m.getPrice()+200);
				sharePrizeDao.mergeMoney(m);
			}else{
				Money money=new Money();
				money.setPrice(200);
				money.setType(1);
				money.setPhone(map.get("phone").toString());
				sharePrizeDao.addMoney(money);
			}
			result.put("success", true);
		} catch (Exception e) {
			result.put("success", false);
		}
		return result;
	}

	@Override
	public List<SearchLog> hasShareLog(String phone) {
		return sharePrizeDao.hasShareLog(phone);
	}

	@Override
	public List<SharePrize> hassharePrize(String phone) {
		return sharePrizeDao.hasSharePrize(phone);
	}

	@Override
	public Map<String, Object> inserShareLog(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SearchLog log=new SearchLog();
			log.setPhone(map.get("phone").toString());
			log.setSearch_time(new Date());
			sharePrizeDao.inserSearchLog(log);
			result.put("success", true);
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Money getMoneyByPhone(Map<String, Object> map){
		Money m=null;
		try {
			m = sharePrizeDao.hasMoney(map.get("phone").toString(),1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return m;
	} 
	
}
